package com.littlebee.storysuper.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.ToastUtil;
import com.littlebee.storysuper.utils.ValidateUtil;
import com.littlebee.storysuper.widget.CountDownTimerUtils;
import com.zss.library.fragment.BaseFragment;
import com.zss.library.utils.LogUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;


public class RegisterFragment extends BaseFragment implements View.OnClickListener{
    EditText et_username;
    EditText et_vericode;
    EditText et_password;
    Button sumbit;
    TextView tv_sendcode;


    @Override
    public int getLayoutResId() {
        return R.layout.fragment_register;
    }

    @Override
    public void initView() {
        super.initView();
        et_username=(EditText)findViewById(R.id.et_username);
        et_vericode=(EditText)findViewById(R.id.et_vericode);
        et_password=(EditText)findViewById(R.id.et_password);
        tv_sendcode=(TextView)findViewById(R.id.tv_sendcode);
        sumbit=(Button)findViewById(R.id.btn_submit);
        tv_sendcode.setOnClickListener(this);
        sumbit.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    public void onSendCodeBtnClicked() {
        String phoneStr = et_username.getText().toString().trim();
        if (TextUtils.isEmpty(phoneStr)) {
            ToastUtil.displayShortToast(R.string.please_input_phone);

            return;
        }
        if (!ValidateUtil.isMobileValid(phoneStr)) {
            ToastUtil.displayShortToast(R.string.please_input_valid_phone);


            return;
        }
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_sendcode, 60000, 1000);
        mCountDownTimerUtils.start();
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("phoneId", phoneStr);
        //loadData(RequestCode.SEND_SMS_CODE_VOCATIONAL_ID, hashMap);
        NetWorkUtils.get(getActivity(),"sendverify", hashMap,true, new NetWorkUtils.IListener(){

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                LogUtils.i("XHX","XHXSUCC:"+result);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX","XHXERROR:"+result);

            }
        });
    }

    public void onSubmitBtnClicked() {
        final String phoneStr = et_username.getText().toString().trim();
        String vericodeStr = et_vericode.getText().toString().trim();
        final String pwdStr = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(phoneStr)) {
            ToastUtil.displayShortToast(R.string.please_input_phone);
            return;
        }
        if (TextUtils.isEmpty(pwdStr)) {
            ToastUtil.displayShortToast(R.string.please_input_password);
            return;
        }
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("phoneId", phoneStr);
        hashMap.put("checkcode", vericodeStr);
        hashMap.put("password", pwdStr);
        //loadData(RequestCode.REGISTER_VOCATIONAL_ID, hashMap);
        NetWorkUtils.get(getActivity(),"register", hashMap,true, new NetWorkUtils.IListener(){

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                LogUtils.i("XHX","XHXSUCC:"+result);
                bingMessage(phoneStr,pwdStr);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX","XHXERROR:"+result);

            }
        });
    }
    /**注册极光推送*/
    private void bingMessage(String userName,String password) {

        JMessageClient.register(userName, password, new BasicCallback() {
            @Override
            public void gotResult(int responseCode, String registerDesc) {
                if (responseCode == 0) {
                    Toast.makeText(getActivity(), "注册成功", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(getActivity(), "注册失败", Toast.LENGTH_SHORT).show();
                    Log.i("RegisterActivity", "JMessageClient.register " + ", responseCode = " + responseCode + " ; registerDesc = " + registerDesc);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_sendcode:
                onSendCodeBtnClicked();
                break;
            case R.id.btn_submit:
                onSubmitBtnClicked();
                break;
        }
    }
}
